package roleserviceimpl;

import bean.Role;
import impl.UserDaoImpl;
import org.apache.commons.dbutils.DbUtils;
import service.RoleService;
import user.UserDao;
import util.DBHelper;
import util.ServerResponseResult;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @classname:RoleServiceImpl
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/31 11:21
 * @version: 0.1
 * @since:1.8
 */
public class RoleServiceImpl implements RoleService {

    @Override
    public ServerResponseResult addRole(Role role, int[] pids) {
        Connection con = DBHelper.getCon();
        try {
            //将自动提交师傅该为手动
            con.setAutoCommit(false);
            UserDao userDao = new UserDaoImpl(con);
            try {
                long l = userDao.addRole(role);
                int i = userDao.addRoleAndPer((int) l, pids);
                //提交事物
                DbUtils.commitAndCloseQuietly(con);
                System.out.println(i);
                return ServerResponseResult.success();
            } catch (Exception e) {
                DbUtils.rollbackAndCloseQuietly(con);

                e.printStackTrace();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ServerResponseResult.error();

    }
}
